In [1]:
import pickle
import numpy as np
import pandas as pd
import os, glob
import seaborn as sns
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold, cross_validate
from sklearn.model_selection import train_test_split
import lightgbm as lgb
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error

## The following are the ML models which can be used for trasinning
from sklearn.model_selection import RandomizedSearchCV
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import RandomForestRegressor,  GradientBoostingRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern
from sklearn.linear_model import LinearRegression

from sklearn.preprocessing import MinMaxScaler,StandardScaler

import timeit
import warnings
warnings.filterwarnings("ignore")
E:\Anaconda3\envs\tf\lib\site-packages\xgboost\compat.py:36: FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead.
  from pandas import MultiIndex, Int64Index
In [2]:
import matplotlib.pyplot as plt
import matplotlib as mpl

import numpy as np
%matplotlib inline
import pandas as pd
In [3]:
import seaborn as sns
sns.set(style="darkgrid")
In [4]:
sns.set_context('talk')
In [5]:
from IPython.display import display, HTML, Image
In [6]:
import matplotlib.colors as mcolors
In [ ]:
 
In [7]:
dataFolder = os.getcwd()
In [8]:
group1_low =1.634
group2_low =0.673
group2_upper =1.634
group3_upper =0.673
In [9]:
## read the test file
data =pd.read_csv(os.path.join(dataFolder,'dataset5H_test.csv'))
data.columns =[col.strip() for col in data.columns]
data['ratio'] = data['b(CaO)']/data['b(SiO2)']
group1 = data[data['ratio']>=group1_low]  # with Portlandite, no Amor-S1
group2 = data[(data['ratio']<=group2_upper) & (data['ratio']>=group2_low)]  # no Portlandite, no Amor-S1
group3 = data[data['ratio']<=group3_upper]     # no Portlandite, with Amor-S1
groups = [group1,group2,group3]
In [10]:
## read trained Model info:
modelInfo =pd.read_csv(os.path.join(dataFolder,'ModelTrainSummary.csv'))
modelInfo = modelInfo.iloc[:,1:4]
modelInfo
Out[10]:
group var modelType
0 group1 Vol(aq) rfModel
1 group1 Vol(aq) xgbModel
2 group1 Vol(aq) lgbModel
3 group1 Vol(aq) gbModel
4 group1 Vol(aq) MLPModel
... ... ... ...
97 group3 nCa(CSHQ) linear
98 group3 nSi(CSHQ) linear
99 group3 nH2O(CSHQ) linear
100 group3 C/S(CSHQ) CONST
101 group3 nGelPW(CSH) linear

102 rows × 3 columns

In [11]:
modelTypeLst = modelInfo['modelType'].unique()
MLModels =[ml for ml in modelTypeLst if 'Model' in ml]
nonMLModels = [ml for ml in modelTypeLst if 'Model' not in ml]
In [ ]:
 
In [12]:
## load the mdoels
dataCols = data.columns.to_list()
modelsLst = []
for irow,row in modelInfo.iterrows():
    group = row['group']
    var =row['var']
    modelType = row['modelType']
    icol =dataCols.index(var)
    if '/' in var:
        fileCol = var.replace("/",'')
    else:
        fileCol = var            

    modelFolder = os.path.join(dataFolder,'SavedModel',group)
    if modelType=='CONST':
        ext = '.csv'
        fileName = os.path.join(modelFolder,modelType + '_'+str(icol)+'_'+fileCol+ext)
        tempModel =pd.read_csv(fileName)
    else:
        ext ='.sav'
        fileName = os.path.join(modelFolder,modelType + '_'+str(icol)+'_'+fileCol+ext)
        tempModel = pickle.load(open(fileName, 'rb'))
    
    modelsLst.append(tempModel)

modelInfo['modelObj'] = modelsLst   
In [ ]:
 
In [13]:
finalDF = pd.DataFrame()
for ML in MLModels:
    modelProcessing =[]
    modelProcessing = nonMLModels.copy()
    modelProcessing.append(ML)
    tempModelInfo = modelInfo[modelInfo['modelType'].isin(modelProcessing)]
    for col in group1.columns[4:-1]:  # exclude 'ratio' 
        colModelInfo = tempModelInfo[tempModelInfo['var'] == col]  
        
        for i, group in enumerate(groups):
        
            dataX = group.iloc[:,1:4]    
            dataY = group[col].values
            groupModelInfo = colModelInfo[colModelInfo['group'] =='group'+str(i+1)]
            modelType = groupModelInfo['modelType'].values[0]
            model = groupModelInfo['modelObj'].values[0]
            if modelType=='CONST':
                y_pred = list(model['const'].values)*len(dataY)
                
            elif modelType=='linear':
                y_pred = model.predict(dataX.values)
            
            else:
                scaler = StandardScaler().fit(dataX.values)
                X_scaled = scaler.transform(dataX.values)  # This will be used for input of trainning dataset
                y_pred  = model.predict(X_scaled)  
    
            tempDF = pd.DataFrame({'testDataY':dataY, 'predDataY':y_pred})
            tempDF['modelType'] = [modelType]*len(tempDF)
            tempDF['var'] = [col]*len(tempDF)
            tempDF['group'] = ['group'+str(i+1)]*len(tempDF)
            tempDF['modelSimulation'] = [ML+'_simulation']*len(tempDF)
                
            if len(finalDF)==0:   
                finalDF = tempDF
            else:
                finalDF = pd.concat([finalDF,tempDF],axis=0)
    
In [14]:
finalDF.to_csv('Comparison_predict_testDataset.csv',index=False)

Visualization of the prediction of the test dataset¶

In [15]:
def drawPlots(trgVar,testDataY,predDataY,simulation):
    
    fig, ax =plt.subplots(figsize=(8,8))
    
    RMSE = mean_squared_error(testDataY,predDataY)
    R2   = r2_score(testDataY,predDataY)
    axMax = max(max(testDataY),max(predDataY))
    axMin = min(min(testDataY),min(predDataY))
    
    ## Fitting the line           
    x2= np.linspace(axMin,axMax,30);
    y2 = x2
    ax.plot(testDataY,predDataY,'bo',markerfacecolor = 'blue',markeredgecolor ='darkblue',markeredgewidth=0.5,label=trgVar)
    
    #ax.plot(df3['b(SiO2)'].values,df3['b(CaO)'].values,'bo',markerfacecolor = 'c',markeredgecolor ='b',markeredgewidth=0.5,label='no Amor-S1')
    #ax.plot(group3['b(SiO2)'].values,group3['b(CaO)'].values,'g^',markerfacecolor = 'm',markeredgecolor ='m',markeredgewidth=1,label='group3')
    ax.plot(x2,y2,'r-',lw=3,label ='1:1 ratio')
    ax.legend (loc='best',ncol=5)           
    ax.set_xlim(axMin,axMax)
    ax.set_ylim(axMin,axMax)
    #ax.text(0.05, 0.8, 'Above the line with Portlandite',fontsize = 20,color = 'k')
    #ax.text(0.4, 0.50, 'Below the line no Portlandite',fontsize = 20,color = 'k')
    ax.set_title(simulation+ ' ====> '+trgVar)
    ax.set_xlabel('testDataY')
    ax.set_ylabel('PredDataY')    
        
    return R2, RMSE    
In [ ]:
 
In [16]:
modelSimulations = finalDF['modelSimulation'].unique()
trgVars = finalDF['var'].unique()
statMeasure = []
for simulation in modelSimulations:
    if 'MLP' in simulation or 'xgb' in simulation: # skip the xgb and MLP models
        continue
    tempDF = finalDF[finalDF['modelSimulation']==simulation]
    for trgVar in trgVars:
        df = tempDF[tempDF['var']==trgVar]  # no Portlandite
        testDataY = df['testDataY'].values
        predDataY = df['predDataY'].values
        R2,RMSE = drawPlots(trgVar,testDataY,predDataY,simulation)
        statMeasure.append([trgVar,R2,RMSE,simulation])
    
statMeasDF = pd.DataFrame(statMeasure,columns=['Var','R2','RMSE','modelSimulation'])    
In [17]:
statMeasDF
Out[17]:
Var R2 RMSE modelSimulation
0 Vol(aq) 0.985275 1.509007e-05 rfModel_simulation
1 pH 0.973782 2.122328e-02 rfModel_simulation
2 nCa(aq) 0.971314 1.651063e-08 rfModel_simulation
3 nCa(s) 1.000000 1.852110e-08 rfModel_simulation
4 nSi(aq) 0.945441 1.082515e-09 rfModel_simulation
5 nSi(s_reac) 1.000000 9.817474e-10 rfModel_simulation
6 nPortlandite 0.975102 3.457005e-03 rfModel_simulation
7 nAmor-Sl 0.939518 5.363642e-04 rfModel_simulation
8 mCSHQ 0.993787 6.185208e-06 rfModel_simulation
9 nCa(CSHQ) 1.000000 1.852106e-08 rfModel_simulation
10 nSi(CSHQ) 1.000000 9.817475e-10 rfModel_simulation
11 nH2O(CSHQ) 0.995279 9.974441e-04 rfModel_simulation
12 C/S(CSHQ) 0.975104 3.218172e-03 rfModel_simulation
13 nGelPW(CSH) 0.999452 2.298766e-05 rfModel_simulation
14 Vol(aq) 0.983603 1.680433e-05 lgbModel_simulation
15 pH 0.950515 4.005849e-02 lgbModel_simulation
16 nCa(aq) 0.969736 1.741881e-08 lgbModel_simulation
17 nCa(s) 1.000000 1.852110e-08 lgbModel_simulation
18 nSi(aq) 0.927125 1.445938e-09 lgbModel_simulation
19 nSi(s_reac) 1.000000 9.817474e-10 lgbModel_simulation
20 nPortlandite 0.976046 3.325987e-03 lgbModel_simulation
21 nAmor-Sl 0.845475 1.370340e-03 lgbModel_simulation
22 mCSHQ 0.992768 7.199778e-06 lgbModel_simulation
23 nCa(CSHQ) 1.000000 1.852106e-08 lgbModel_simulation
24 nSi(CSHQ) 1.000000 9.817475e-10 lgbModel_simulation
25 nH2O(CSHQ) 0.993060 1.466399e-03 lgbModel_simulation
26 C/S(CSHQ) 0.970789 3.775929e-03 lgbModel_simulation
27 nGelPW(CSH) 0.999452 2.298766e-05 lgbModel_simulation
28 Vol(aq) 0.975132 2.548530e-05 gbModel_simulation
29 pH 0.957225 3.462690e-02 gbModel_simulation
30 nCa(aq) 0.959168 2.350142e-08 gbModel_simulation
31 nCa(s) 1.000000 1.852110e-08 gbModel_simulation
32 nSi(aq) 0.941068 1.169279e-09 gbModel_simulation
33 nSi(s_reac) 1.000000 9.817474e-10 gbModel_simulation
34 nPortlandite 0.979012 2.914139e-03 gbModel_simulation
35 nAmor-Sl 0.687053 2.775241e-03 gbModel_simulation
36 mCSHQ 0.990805 9.154313e-06 gbModel_simulation
37 nCa(CSHQ) 1.000000 1.852106e-08 gbModel_simulation
38 nSi(CSHQ) 1.000000 9.817475e-10 gbModel_simulation
39 nH2O(CSHQ) 0.991723 1.748774e-03 gbModel_simulation
40 C/S(CSHQ) 0.952196 6.179314e-03 gbModel_simulation
41 nGelPW(CSH) 0.999452 2.298766e-05 gbModel_simulation
42 Vol(aq) 0.990621 9.611760e-06 GPyModel_simulation
43 pH 0.993751 5.058884e-03 GPyModel_simulation
44 nCa(aq) 0.992307 4.428142e-09 GPyModel_simulation
45 nCa(s) 1.000000 1.852110e-08 GPyModel_simulation
46 nSi(aq) 0.922855 1.530658e-09 GPyModel_simulation
47 nSi(s_reac) 1.000000 9.817474e-10 GPyModel_simulation
48 nPortlandite 0.977159 3.171430e-03 GPyModel_simulation
49 nAmor-Sl 0.964001 3.192405e-04 GPyModel_simulation
50 mCSHQ 0.995794 4.187317e-06 GPyModel_simulation
51 nCa(CSHQ) 1.000000 1.852106e-08 GPyModel_simulation
52 nSi(CSHQ) 1.000000 9.817475e-10 GPyModel_simulation
53 nH2O(CSHQ) 0.996719 6.931502e-04 GPyModel_simulation
54 C/S(CSHQ) 0.996542 4.469448e-04 GPyModel_simulation
55 nGelPW(CSH) 0.999452 2.298766e-05 GPyModel_simulation
In [18]:
statMeasDF_pivot=statMeasDF.pivot(index='Var',values=['R2','RMSE'],columns='modelSimulation')
statMeasDF.to_csv('statMeasure_trainDataset_noPivot.csv',index = False)

statMeasDF_pivot.to_csv('statMeasure_trainDataset_Pivot.csv',index = False)
In [19]:
## alternative way to draw figures
In [ ]:
 
In [20]:
def drawCombiningPlots(plotDataDF,trgVar):
    
    fig, ax =plt.subplots(figsize=(10,10))
    
    marker = ['o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X']
    colors = ['tab:blue','tab:orange','tab:gray','tab:olive','tab:cyan','tab:green']
    ## Fitting the line           
    axisMin=1000
    axisMax =-1000
    for i, col in enumerate(plotDataDF.columns[1:]):
        testDataY = plotDataDF.iloc[:,0].values
        predDataY = plotDataDF[col].values
        axMax = max(max(testDataY),max(predDataY))
        axMin = min(min(testDataY),min(predDataY))
        axisMin = min(axisMin,axMin)
        axisMax = max(axisMax,axMax)
        ax.scatter(testDataY,predDataY,marker =marker[i],edgecolor =colors[i],label=col.replace('_simulation',''))

    x2= np.linspace(axisMin,axisMax,30);
    y2 = x2
    ax.plot(x2,y2,'r-',lw=3,label ='1:1 ratio')
    ax.legend (loc='best',ncol=3)           
    ax.set_xlim(axisMin,axisMax)
    ax.set_ylim(axisMin,axisMax)
    ax.set_title(trgVar)
    ax.set_xlabel('testDataY')
    ax.set_ylabel('PredDataY')    
        
In [21]:
modelSimulations = finalDF['modelSimulation'].unique()
trgVars = finalDF['var'].unique()
for trgVar in trgVars:
    tempdf = finalDF[finalDF['var']==trgVar]  # no Portlandite
    dataplot = {}
    plotDataDF = pd.DataFrame()
    for simulation in modelSimulations:
        # skip the xgb and MLP models
        # the two models are very bad, need to be fine tune
        if 'MLP' in simulation or 'xgb' in simulation: 
            continue

        df = tempdf[tempdf['modelSimulation']==simulation]
        if len(plotDataDF)==0:
            plotDataDF=df[['testDataY','predDataY']]
        else:    
            plotDataDF = pd.concat([plotDataDF,df[['predDataY']]],axis=1)

        plotDataDF.rename({'predDataY': simulation}, axis=1,inplace=True)
                                    
    drawCombiningPlots(plotDataDF,trgVar)    
 
In [ ]: